package ywpt.service.config;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import ywpt.core.dao.HibernateEntityDao;
import ywpt.model.ContractType;
import ywpt.web.common.PublicFunction;

public class ContractTypeServiceImpl extends HibernateEntityDao<ContractType> implements ContractTypeService{

	public ContractTypeServiceImpl(){
		
	}
	
	@Override
	public int getContractTypeCountByCriteria(DetachedCriteria query) {
		return this.getCountByCriteria(query);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ContractType> getContractTypes(DetachedCriteria query,
			int pageSize, int startIndex) {
		return this.findAllByCriteria(query, pageSize, startIndex);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ContractType> getContractTypesByTypeName(String typeName,Long classId, int start, int size) {
		DetachedCriteria query = DetachedCriteria.forClass(ContractType.class, "contractType");
		query.add(Restrictions.eq("classId", classId));
		if ( typeName != null && !typeName.trim().equals("") )
		{
			query.add(Restrictions.or(Restrictions.like("typeName", "%" + typeName + "%"),  
							          Restrictions.like("typeCode", "%" + typeName + "%")));
				// .add(Restrictions.eq("organType", "01"));
		}
		query.addOrder(Order.asc("typeCode"));
		List<ContractType> contractTypeList = new ArrayList<ContractType>();
		List<ContractType> contractTypeLists = new ArrayList<ContractType>();
		ContractType contractType =new ContractType();
		
		contractTypeList.addAll(this.findAllByCriteria(query, size, start));
		int i = 1;
		for ( Iterator<ContractType> itr = contractTypeList.iterator(); itr.hasNext(); i++ ) 
		{
			contractType = itr.next();
			String ss = PublicFunction.format(contractType.getTypeCode() + "", 6, "  ", 1)
			          //+ PublicFunction.format(organ.getOrganName() + "", 14, "  ", 1);
			          + contractType.getTypeName(); 
			
			contractType.setTypeName(ss);
			contractTypeLists.add(contractType);
		}
		return contractTypeLists;
	}

	@Override
	public ContractType getContractTypeById(Long id) {
		return this.get(id);
	}	
	@Override
	public int getContractTypesCountByName(String typeName,Long classId ) {
		DetachedCriteria query = DetachedCriteria.forClass(ContractType.class, "contractType");
		query.add(Restrictions.eq("classId", classId));
		if ( typeName != null && !typeName.trim().equals("") )
		{
			query.add(Restrictions.or(Restrictions.like("typeName", "%" + typeName + "%" ),  
							          Restrictions.like("typeCode", "%" + typeName + "%" )));
				 //.add(Restrictions.eq("organType", "01"));
		}
		query.setProjection(null);
		return this.getCountByCriteria(query);
	}

	
	@Override
	public String CreateTypeCode() {
		DetachedCriteria query = DetachedCriteria.forClass(ContractType.class, "contractType");
		String typeCode="";
		String code="";
		Integer maxId=new Integer(0);
		query.addOrder(Order.desc("id"));
		List<ContractType> contractTypes = this.getContractTypes(query);
		if ( contractTypes.iterator().hasNext() )	
		{
			code = (contractTypes.iterator().next().getTypeCode());
			maxId = new Integer(code);
		}
		typeCode=PublicFunction.getNextCode("", maxId, false,3); 
		query.setProjection(null);
		return typeCode;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ContractType> getContractTypes(DetachedCriteria query) {
		return this.findAllByCriteria(query);
	}

	@Override
	public void saveContractType(ContractType contractType) {
		this.save(contractType);
	}

	@Override
	public void updateContractType(ContractType contractType) {
		this.update(contractType);
	}

	@Override
	public void removeContractType(Long id) {
		this.removeById(id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Long> getClassGroup() {
		String hql="select distinct(classId) from ContractType";
		return this.find(hql);
	}

	
}
